package com.crmweb.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.crmweb.entity.Askers;
import com.crmweb.entity.PageVo;
import com.crmweb.entity.Result;
import com.crmweb.entity.Students;
import com.crmweb.entity.Users;
import com.crmweb.service.StudentService;
import com.crmweb.tool.ConstantUtil;

import io.lettuce.core.dynamic.annotation.Param;

@RestController
public class StudentController {

	@Resource
	private StudentService stus;
	//查询所有咨询师
	@RequestMapping("netFile/selAllAsker")
	public Map<String, Object> selAllAsker(HttpServletRequest request){
		List<Askers> aslist = stus.selAllAsker();
		Map<String, Object> map = new HashMap<>();
		map.put("aslist", aslist);
		return map;
		
	}
	//多条件分页查询咨询师 
	//是咨询师就查询当前网络咨询师管理的学生
	//咨询师经理查询所有学生
	@RequestMapping("netFile/selAllByAskerid")
	public Map<String, Object> selAllByAskerid(HttpServletRequest request){
		
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String askerId = request.getParameter("askerId");
		HttpSession session = request.getSession();
		Users user = (Users) session.getAttribute(ConstantUtil.USERS); 
		//网络咨询师名字
		String createUser = user.getLoginName();
		//System.out.println("---------------"+createUser);
		String isPay = request.getParameter("isPay");
		String isValid = request.getParameter("isValid");
		String isReturnVist = request.getParameter("isReturnVist");
		String qq = request.getParameter("qq");
		String sort = request.getParameter("sort");
		
		String page = request.getParameter("page");
		String pageSize = request.getParameter("limit");
		Students stu = new Students();
		if (createUser !=""&& createUser !=null) {
			stu.setCreateUser(createUser);
		}
		if (name !=""&& name !=null) {
			stu.setName(name);
		}
		if (phone !=""&& phone !=null) {
			stu.setPhone(phone);
		}
		if (askerId !=""&& askerId !=null) {
			stu.setAskerId(askerId);
		}
		if (isPay !=""&& isPay !=null) {
			stu.setIsPay(isPay);
		}
		if (isValid !=""&& isValid !=null) {
			stu.setIsValid(isValid);
		}
		if (isReturnVist !=""&& isReturnVist !=null) {
			stu.setIsReturnVist(isReturnVist);
		}
		if(qq !="" && qq !=null) {
			stu.setQq(qq);
		}
		if (sort !=""&& sort !=null) {
			stu.setSort(sort);
		}
		
		PageVo vo = new PageVo();
		vo.setPage(Integer.parseInt(page));
		vo.setPageSize(Integer.parseInt(pageSize));
//		System.out.println(page);
//		System.out.println(pageSize);
		stu.setVo(vo);
		
		List<Students> lists = stus.selAllByAskerid(stu);
		Map<String, Object > map = new HashMap<>();
		map.put("code", 0);
		map.put("data", lists);
		map.put("count", stus.getCountByWhere(stu));
		
		return map;
		
	}
	@RequestMapping("netFile/addStu")
	public Object addStu(Students stu) {
		/*
		 * HttpSession session = request.getSession(); Users user = (Users)
		 * session.getAttribute(ConstantUtil.USERS); //网络咨询师名字 String createUser =
		 * user.getLoginName(); stu.setCreateUser(createUser);
		 * System.out.println("++++++====-"+stu.getCreateUser());
		 */
		int i = stus.addStu(stu);
		if(i>0) {
			Result res = new Result("添加成功","",true);
			return res;
		}else {
			Result res = new Result("添加失败","",false);
			return res;
		}
		
		
	}
	
	//修改学生信息
	@RequestMapping("netFile/updateStudent")
	public Object updateStudent(Students stu) {
		/*
		 * HttpSession session = request.getSession(); Users user = (Users)
		 * session.getAttribute(ConstantUtil.USERS); //网络咨询师名字 String createUser =
		 * user.getLoginName(); stu.setCreateUser(createUser);
		 * System.out.println("++++++====-"+stu.getCreateUser());
		 */
		System.out.println("_---------------"+stu.getHomeTime());
		System.out.println("_---------------"+stu.getId());
		System.out.println(stu.getName());
		int i = stus.updateStudent(stu);
		
		if(i>0) {
			Result res = new Result("修改成功","",true);
			return res;
		}else {
			Result res = new Result("修改失败","",false);
			return res;
		}
		
		
	}
	@RequestMapping("netFile/updsStuAskerBySid")
	public Object updsStuAskerBySid(@RequestParam("ids")List<Integer> ids,@RequestParam("askerId")String askerId) {
		//System.out.println("---------------"+ids);
		int i = stus.updsStuAskerBySid(ids, askerId);
		if(i>0) {
			Result res = new Result("咨询师分配成功","",true);
			return res;
		}else {
			Result res = new Result("咨询师分配失败","",false);
			return res;
		}
	}
	//删除学生记录
	@RequestMapping("netFile/deleteStudent")
	public Object deleteStudent(int id) {
		
		int i = stus.deleteStudent(id);
		if(i>0) {
			Result res = new Result("删除成功","",true);
			return res;
		}else {
			Result res = new Result("删除失败","",false);
			return res;
		}
		
		
	}
	//咨询师经理查询所有学生
		@RequestMapping("netFile/selAllStudent")
		public Map<String, Object> selAllStudent(HttpServletRequest request){
			
			String name = request.getParameter("name");
			String phone = request.getParameter("phone");
			String askerId = request.getParameter("askerId");
//			HttpSession session = request.getSession();
//			Users user = (Users) session.getAttribute(ConstantUtil.USERS); 
//			//网络咨询师名字
//			String createUser = user.getLoginName();
			//System.out.println("---------------"+createUser);
			String isPay = request.getParameter("isPay");
			String isValid = request.getParameter("isValid");
			String isReturnVist = request.getParameter("isReturnVist");
			String qq = request.getParameter("qq");
			String sort = request.getParameter("sort");
			
			String page = request.getParameter("page");
			String pageSize = request.getParameter("limit");
			Students stu = new Students();
//			if (createUser !=""&& createUser !=null) {
//				stu.setCreateUser(createUser);
//			}
			if (name !=""&& name !=null) {
				stu.setName(name);
			}
			if (phone !=""&& phone !=null) {
				stu.setPhone(phone);
			}
			if (askerId !=""&& askerId !=null) {
				stu.setAskerId(askerId);
			}
			if (isPay !=""&& isPay !=null) {
				stu.setIsPay(isPay);
			}
			if (isValid !=""&& isValid !=null) {
				stu.setIsValid(isValid);
			}
			if (isReturnVist !=""&& isReturnVist !=null) {
				stu.setIsReturnVist(isReturnVist);
			}
			if(qq !="" && qq !=null) {
				stu.setQq(qq);
			}
			if (sort !=""&& sort !=null) {
				stu.setSort(sort);
			}
			
			PageVo vo = new PageVo();
			vo.setPage(Integer.parseInt(page));
			vo.setPageSize(Integer.parseInt(pageSize));
//			System.out.println(page);
//			System.out.println(pageSize);
			stu.setVo(vo);
			
			List<Students> lists = stus.selAllByAskerid(stu);
			Map<String, Object > map = new HashMap<>();
			map.put("code", 0);
			map.put("data", lists);
			map.put("count", stus.getCountByWhere(stu));
			
			return map;
			
		}
		//多条件分页查询咨询师 
		//是咨询师就查询当前咨询师管理的学生
		//咨询师经理查询所有学生
		@RequestMapping("netFile/selAllByAid")
		public Map<String, Object> selAllByAid(HttpServletRequest request){
			
			String name = request.getParameter("name");
			String phone = request.getParameter("phone");
			//String askerId = request.getParameter("askerId");
			HttpSession session = request.getSession();
			Users user = (Users) session.getAttribute(ConstantUtil.USERS); 
			String askerId = user.getId();
//			//网络咨询师名字
//			String createUser = user.getLoginName();
			//System.out.println("---------------"+createUser);
			String isPay = request.getParameter("isPay");
			String isValid = request.getParameter("isValid");
			String isReturnVist = request.getParameter("isReturnVist");
			String qq = request.getParameter("qq");
			String sort = request.getParameter("sort");
			
			String page = request.getParameter("page");
			String pageSize = request.getParameter("limit");
			Students stu = new Students();
//			if (createUser !=""&& createUser !=null) {
//				stu.setCreateUser(createUser);
//			}
			if (name !=""&& name !=null) {
				stu.setName(name);
			}
			if (phone !=""&& phone !=null) {
				stu.setPhone(phone);
			}
			if (askerId !=""&& askerId !=null) {
				stu.setAskerId(askerId);
			}
			if (isPay !=""&& isPay !=null) {
				stu.setIsPay(isPay);
			}
			if (isValid !=""&& isValid !=null) {
				stu.setIsValid(isValid);
			}
			if (isReturnVist !=""&& isReturnVist !=null) {
				stu.setIsReturnVist(isReturnVist);
			}
			if(qq !="" && qq !=null) {
				stu.setQq(qq);
			}
			if (sort !=""&& sort !=null) {
				stu.setSort(sort);
			}
			
			PageVo vo = new PageVo();
			vo.setPage(Integer.parseInt(page));
			vo.setPageSize(Integer.parseInt(pageSize));
//			System.out.println(page);
//			System.out.println(pageSize);
			stu.setVo(vo);
			
			List<Students> lists = stus.selAllByAskerid(stu);
			Map<String, Object > map = new HashMap<>();
			map.put("code", 0);
			map.put("data", lists);
			map.put("count", stus.getCountByWhere(stu));
			
			return map;
			
		}
}
